<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8"/>
    <title>列表</title>
    <link href="__STATIC__/component/pear/css/pear.css" rel="stylesheet"/>
    {include file='public/head.html'}
</head>
<body class="pear-container">

<div class="layui-card">
    <div class="layui-card-body">
        <table class="layui-hide" id="currentTable" lay-filter="currentTable"
               data-auth-add="{:check_auth('add')}"
               data-auth-edit="{:check_auth('edit')}"
               data-auth-delete="{:check_auth('del')}"
               data-auth-modify="{:check_auth('multi')}"
               data-auth-field="{:check_auth('model_field/index')}"
        ></table>
    </div>
</div>

<script src="__STATIC__/component/layui/layui.js"></script>
<script src="__STATIC__/component/pear/pear.js"></script>
<script>

    layui.use(['aphpTable', 'table', 'dropdown', 'aphp'], function () {
        var aphpTable = layui.aphpTable;
        var $ = layui.$;
        var table = layui.table;
        var aphp = layui.aphp;
        var dropdown = layui.dropdown;

        var init = {
            table_elem: '#currentTable',
            table_render_id: 'currentTable',
            add_url: "{:url('add')}",
            edit_url: "{:url('edit')}",
            delete_url: "{:url('del')}",
            modify_url: "{:url('multi')}",
        };

        aphpTable.render({
            init: init,
            url: "{:url('index')}",
            toolbar: ['refresh_table', 'add', 'enable', 'disable', 'delete'],
            search: false,
            cols: [[
                { type: 'checkbox'},
                { title: '排序', field: 'sort', search: false, edit: 'text'},
                { title: 'ID', field: 'id' },
                { title: '模型', field: 'title' },
                { title: '标识', field: 'table_name' },
                { title: '更新时间', field: 'update_time', templet: aphpTable.formatter.datetime },
                { title: '状态', field: 'status', align: 'center', unresize: true, templet: aphpTable.formatter.switch },
                { title: '操作', align: 'center', width:330, templet: aphpTable.formatter.tool, operat: [
                        [{
                            title: '模型字段',
                            text: '字段',
                            class: 'layui-btn layui-btn-primary layui-btn-sm',
                            icon: 'layui-icon layui-icon-app',
                            url: "{:url('model_field/index')}",
                            method: 'open',
                            auth: 'field',
                            field_name: 'model_id',
                            extend: 'data-full="true"'
                        }],
                        [{
                            title: '操作',
                            text: '操作 <i class="layui-icon layui-icon-down"></i>',
                            class: 'layui-btn layui-btn-normal layui-btn-sm',
                            method: 'none',
                            extend: 'data-event="operate"',
                        }],
                        [{
                            title: '生成',
                            text: '生成 <i class="layui-icon layui-icon-down"></i>',
                            class: 'layui-btn layui-btn-sm',
                            method: 'none',
                            extend: 'data-event="make"',
                        }],
                        [{
                            title: '移除',
                            text: '移除 <i class="layui-icon layui-icon-down"></i>',
                            class: 'layui-btn layui-btn-danger layui-btn-sm',
                            method: 'none',
                            extend: 'data-event="remove"',
                        }],
                    ]}
            ]],
            page: {}
        });

        aphpTable.bindEvent();

        $(document).on('click', "[data-event='operate']", function() {
            var model_id = $(this).data('id');
            dropdown.render({
                elem: this, // 触发事件的 DOM 对象
                show: true, // 外部事件触发即显示
                data: [{
                    title: '修改',
                    id: 'edit'
                },{
                    title: '删除',
                    id: 'del'
                }],
                click: function(menu){
                    if(menu.id === 'edit'){
                        aphp.open(menu.title, init.edit_url+'?id='+model_id, 800, 600);
                    }else if(menu.id === 'del'){
                        aphp.request.getConfirm(init.delete_url+'?ids='+model_id, '确认删除所有模型数据？建议先备份再删除', 1);
                    }
                }
            });
        });

        $(document).on('click', "[data-event='make']", function() {
            var model_id = $(this).data('id');
            dropdown.render({
                elem: this, // 触发事件的 DOM 对象
                show: true, // 外部事件触发即显示
                data: [{
                    title: '生成表',
                    id: 'make_table',
                    url: "{:url('make_table')}",
                    confirm: '确认要生成表吗？'
                },{
                    title: '生成MVC',
                    id: 'make_mvc',
                    url: "{:url('make_mvc')}",
                    confirm: '是否一键生成控制器，模型，视图？'
                },{
                    title: '生成权限',
                    id: 'make_auth',
                    url: "{:url('make_auth')}",
                    confirm: '确认要生成权限吗，注意给角色配置权限？'
                },{
                    title: '生成菜单',
                    id: 'make_menu',
                    url: "{:url('make_menu')}",
                    confirm: '确认要生成菜单吗？'
                }],
                click: function(menu){
                    var confirm = menu.confirm || '确认要'+menu.title+'吗？'
                    aphp.request.getConfirm(menu.url+'?model_id='+model_id, confirm);
                }
            });
        });

        $(document).on('click', "[data-event='remove']", function() {
            var model_id = $(this).data('id');
            dropdown.render({
                elem: this, // 触发事件的 DOM 对象
                show: true, // 外部事件触发即显示
                data: [{
                    title: '移除表',
                    id: 'remove_table',
                    url: "{:url('remove_table')}"
                },{
                    title: '移除MVC',
                    id: 'remove_mvc',
                    url: "{:url('remove_mvc')}"
                },{
                    title: '移除权限',
                    id: 'remove_auth',
                    url: "{:url('remove_auth')}"
                },{
                    title: '移除菜单',
                    id: 'remove_menu',
                    url: "{:url('remove_menu')}"
                }],
                click: function(menu){
                    aphp.request.getConfirm(menu.url+'?model_id='+model_id, '确认要'+menu.title+'吗？');
                }
            });
        });

    });
</script>
</body>
</html>